<template>
  <div>
    <el-container>
      <el-header>
        <div class="searchStyle">
          <div class="firstSytle">
            <div>
              <el-button size="mini" type="success" @click="initData" icon="el-icon-refresh">刷新</el-button>
              <el-button size="mini" type="danger" @click="deleteBatch" icon="el-icon-delete">删除</el-button>
            </div>
            <div>
              <el-input size="mini" v-model="searchBuildId" placeholder="请选择公寓号" class="inputStyle"></el-input>
              <el-input size="mini" v-model="searchRoomId" placeholder="请选择房间号"  class="inputStyle"></el-input>
              <el-input size="mini" v-model="searchApplicant" placeholder="请输入报修人"  class="inputStyle"></el-input>
              <el-input size="mini" v-model="searchTime" placeholder="请选择报修时间"  class="inputStyle"></el-input>
              <el-button
                size="mini" type="primary" @click="searchAll">查询
              </el-button>
              <el-button
                size="mini" type="primary" @click="emptyInput">重置
              </el-button>
            </div>

          </div>

        </div>

      </el-header>

      <el-main style="padding-left: 1px;padding-top: 20px;height:100%">
        <div>
          <el-table :data="repairs" stripe v-loading="tableLoading" size="mini" style="width: 100%;" @selection-change="selectDel">
            <el-table-column type="selection" width="30"></el-table-column>
            <el-table-column prop="listId" label="序号" width="80px" align="center"></el-table-column>
            <el-table-column prop="buildId" label="公寓编号" width="150px"></el-table-column>
            <el-table-column prop="roomId" label="宿舍号"></el-table-column>
            <el-table-column prop="description" label="报修描述"></el-table-column>
            <el-table-column prop="status" label="维修状态"></el-table-column>
            <el-table-column prop="time" label="报修时间" width="260px"></el-table-column>
            <el-table-column prop="applicant" label="报修人" width="200px" style="text-align: center"></el-table-column>
            <el-table-column label="操作" align="center" width="200px"> 
              <template slot-scope="scope">
                <el-button type="primary" size="mini" @click="handle(scope.row)">确认维修</el-button>
              </template>
            </el-table-column>
          </el-table>
          <div class="lastLine" style="margin-top: 10px">               
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                           :current-page.sync="pageNum" :page-sizes="[10, 20,30,50,100,200,]"
                           :page-size="pageSize"
                           layout="sizes, prev, pager, next" :total="total">      
            </el-pagination>
          </div>
        </div>
      </el-main>
    </el-container>

  </div>

</template>

<script>
    import {getRequest, putMethod, postRequest, putRequest} from "../../utils/api";

    export default {
        data() {
            return {
                repair: {
                    id: '',
                    buildId: '',
                    roomId: '',
                    status: '',
                    description: '',
                    time: '',
                    applicant: '',
                    operator: '',
                    endTime: '',
                },
                repairs: [],
                tableLoading: false,
                pageNum: 1,
                pageSize: 10,
                total: 0,
                list: [],
                searchRoomId: '',
                searchBuildId: '',
                searchTime: '',
                searchApplicant: '',
                requestObj: {
                    orderConditions: [],
                    pageNum: 1, //当前页数
                    pageSize: 10, //每页条数
                    whereConditions: [],
                },
                tableId:[],

            };
        },
        methods: {
            initData: function () {
                this.tableLoading = true;
                var tempdate
                // var _this = this;
                this.getRequest("/repair/rep/pending/list?pageSize=" + this.pageSize + "&pageNum=" + this.pageNum).then(resp => {
                    this.tableLoading = false;
                    if (resp && resp.status == 200) {
                        this.total = resp.data.obj.total;
                        this.repairs = resp.data.obj.data;
                        for (var i = 0; i < this.repairs.length; i++) {
                            this.repairs[i].listId = i + 1
                            tempdate =this.repairs[i].time
                            this.repairs[i].time=tempdate.slice(0,tempdate.indexOf("."))
                            switch (this.repairs[i].status) {
                                case 0:
                                    this.repairs[i].status = '待维修';
                                    break;
                                case 1:
                                    this.repairs[i].status = '已维修';
                                    break;
                            }
                        }
                    }
                })
            },
            deleteId(row) {
                var id = row.id
                this.tableLoading = true;
                this.deleteRequest("/repair/rep/deleteById/" + id).then(resp => {
                    this.tableLoading = false;
                    if (resp && resp.status == 200) {
                        this.initData();
                        Message.success({message: data.data.msg});
                    }
                })
            },

            handle(row) {
                this.tableLoading = true;
                putMethod("/repair/rep/handle/"+row.id).then(resp => {
                    this.tableLoading = false;
                    if (resp && resp.status == 200) {
                        this.initData();
                        Message.success({message: data.data.msg});
                    }else {
                        this.initData();
                        Message.error({message: data.data.msg});
                    }
                })

            },

            deleteBatch() {
              if(this.tableId.length===0){
                    this.$message.error('请选择要删除的项目');
                    return false;
                }
                this.deleteRequest("/repair/rep/deleteBatch/" + this.tableId).then(resp => {
                      this.tableLoading = false;
                      if (resp && resp.status == 200) {
                          this.initData();
                          // Message.success({message: data.data.msg});
                      }
                  })

            },
            selectDel(row){
              if(row.length!=0){
                for(var i=0;i<row.length;i++){
                  this.tableId[i]=row[i].id;
                }
                  this.tableId= this.tableId.slice(0,row.length)
              }else if(row.length == 0){
                  this.tableId=row
              }
            },
            searchAll() {
                this.initDataParms();
                this.tableLoading = true;
                postMethod("/repair/rep/findByKeyWords", this.requestObj).then(resp => {
                    this.tableLoading = false;
                    if (resp && resp.status == 200) {
                        this.total = resp.data.data.total
                        this.repairs = resp.data.data.data;
                        for (var i = 0; i < this.repairs.length; i++) {
                            this.repairs[i].listId = i + 1
                            switch (this.repairs[i].status) {
                                case 0:
                                    this.repairs[i].status = '待维修';
                                    break;
                                case 1:
                                    this.repairs[i].status = '已维修';
                                    break;
                            }
                        }
                    }
                })

            },

            initDataParms() {
                this.getSort();
                this.requestObj.whereConditions = [];
                if (this.searchBuildId !== '') {
                    this.requestObj.whereConditions.push({
                        field: "buildId",
                        method: 'Equal',
                        value: this.searchBuildId,
                    })
                }
                if (this.searchRoomId !== '') {
                    this.requestObj.whereConditions.push({
                        field: "roomId",
                        method: 'Equal',
                        value: this.searchRoomId,
                    })
                }
                if (this.searchApplicant !== '') {
                    this.requestObj.whereConditions.push({
                        field: "applicant",
                        method: 'Equal',
                        value: this.searchApplicant,
                    })
                }
                if (this.searchTime !== '') {
                    this.requestObj.whereConditions.push({
                        field: "time",
                        method: 'Equal',
                        value: this.searchTime,
                    })
                }
            },
            getSort() {
                this.requestObj.orderConditions.push({
                    field: 'roomId',
                    method: 'ASC',
                });
                this.requestObj.orderConditions.push({
                    field: 'time',
                    method: 'DESC',
                });
            },

            handleSizeChange(val) {    //分页
                this.pageSize = val
                this.initData()
            },
            handleCurrentChange(val) {   //分页
                this.pageNum = val
                this.initData()
            },
            emptyInput() {
                this.searchRoomId = '';
                this.searchBuildId = '';
                this.searchTime = '';
                this.searchApplicant = '';
                this.initData();
            },
        },

        mounted: function () {
            this.initData();
        },

    }
</script>

<style scoped>
  .searchStyle{
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: center;
  }
  .firstSytle{
    margin-top: 10px;
    width: 100%;
    display: flex;
    flex-direction: row;
    justify-content:space-between ;
  }
  .inputStyle{
    width: 140px;
    margin-right: 5px;
  }
  .secondStyle{
    margin-top:10px ;
    width: 100%;
    display: flex;
    flex-direction: row;
    justify-content:space-between ;
  }
  .lastLine {
  display:  flex;
  flex-direction:  row;
  justify-content:  flex-end;
  margin:  10px 0 10px 0;
  }

  .el-row {
    margin: 10px 0;
  }

  .el-table {
    text-align: left;
    size: auto;
    width: 150px;
  }
</style>
